//
//  YGEmoticonModel.swift
//  表情键盘
//
//  Created by liyonggui on 16/5/23.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

/// 表情模型，图片表情模型和Emoji表情模型
class YGEmoticonModel: NSObject, NSCoding {
    
    /// 模型所在文件夹名称
    var id: String
    
    /// chs, 网络传输使用
    var chs: String?
    
    /// png, 客户端显示的图片
    var png: String? {
        didSet {
            imagePath = emoticonBundlePath + "/" + id + "/" + png!
        }
    }
    
    /// 图片全路径
    var imagePath: String?
    
    /// code, emoji的16进制字符串
    var code: String? {
        didSet {
            // 将code转成emoji
            // 扫描器
            let scanner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            // 扫描16进制的的字符串为UInt32
            scanner.scanHexInt(&result)
            
//            print("result: \(result)")
            
            let c = Character(UnicodeScalar(result))
            
            emoji = String(c)
        }
    }
    
    /// emoji 表情
    var emoji: String?
    
    // 字典转模型
    init(dic: [String: String], id: String) {
        self.id = id
        super.init()
        
        setValuesForKeysWithDictionary(dic)
    }
    
    // 没有对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // 打印方法
    override var description: String {
        get {
            return "\n\t\t 表情模型chs: \(chs), png: \(png), code: \(code)"
        }
    }
    
    // MARK: - 实现NSCoding协议方法
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        chs = aDecoder.decodeObjectForKey("chs") as? String
        png = aDecoder.decodeObjectForKey("png") as? String
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        
        if let p = png {
            imagePath = emoticonBundlePath + "/" + id + "/" + p
        }
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }

}



























